extends Node

signal language_changed(language_code)

# 当前语言
var current_language = "zh"
# 翻译数据
var translations = {}

func _ready():
	# 加载语言配置
	load_translations()
	
	# 获取系统语言或之前保存的语言设置
	var saved_language = load_saved_language()
	if saved_language:
		current_language = saved_language
	else:
		# 尝试使用系统语言，如果支持的话
		var system_language = OS.get_locale().substr(0, 2)
		if system_language in translations:
			current_language = system_language
	
	# 应用语言
	_apply_language()

# 加载所有翻译
func load_translations():
	# 中文翻译
	var zh = {
		"main_menu.title": "植物大战怪物",
		"main_menu.start": "开始游戏",
		"main_menu.settings": "设置",
		"main_menu.language": "语言",
		"main_menu.quit": "退出",
		"common.back": "返回",
		"game.skill.attack": "攻击",
		"game.skill.heal": "治疗",
		"game.skill.shield": "护盾",
		"game.skill.poison": "毒素",
		"game.skill.fire": "火焰",
		"game.skill.ice": "冰冻",
		"game.plant.sunflower": "向日葵",
		"game.plant.peashooter": "豌豆射手",
		"game.plant.wallnut": "坚果墙",
		"game.monster.zombie": "普通僵尸",
		"game.monster.cone": "路障僵尸",
		"game.monster.bucket": "铁桶僵尸",
		"game.ui.score": "分数: %d",
		"game.ui.wave": "波数: %d",
		"game.ui.health": "生命: %d",
	}
	
	# 英文翻译
	var en = {
		"main_menu.title": "Plants vs Monsters",
		"main_menu.start": "Start Game",
		"main_menu.settings": "Settings",
		"main_menu.language": "Language",
		"main_menu.quit": "Quit",
		"common.back": "Back",
		"game.skill.attack": "Attack",
		"game.skill.heal": "Heal",
		"game.skill.shield": "Shield",
		"game.skill.poison": "Poison",
		"game.skill.fire": "Fire",
		"game.skill.ice": "Ice",
		"game.plant.sunflower": "Sunflower",
		"game.plant.peashooter": "Peashooter",
		"game.plant.wallnut": "Wall-nut",
		"game.monster.zombie": "Zombie",
		"game.monster.cone": "Cone Zombie",
		"game.monster.bucket": "Bucket Zombie",
		"game.ui.score": "Score: %d",
		"game.ui.wave": "Wave: %d",
		"game.ui.health": "Health: %d",
	}
	
	# 注册翻译
	translations["zh"] = zh
	translations["en"] = en

# 获取保存的语言设置
func load_saved_language():
	if FileAccess.file_exists("user://settings.cfg"):
		var config = ConfigFile.new()
		config.load("user://settings.cfg")
		return config.get_value("settings", "language", "")
	return ""

# 保存语言设置
func save_language_setting():
	var config = ConfigFile.new()
	if FileAccess.file_exists("user://settings.cfg"):
		config.load("user://settings.cfg")
	config.set_value("settings", "language", current_language)
	config.save("user://settings.cfg")

# 设置语言
func set_language(language_code):
	if language_code in translations:
		current_language = language_code
		_apply_language()
		save_language_setting()
		emit_signal("language_changed", language_code)
	else:
		print("警告: 不支持的语言: ", language_code)

# 获取文本
func get_text(key, args = []):
	if not current_language in translations:
		print("警告: 当前语言未注册: ", current_language)
		return key
	
	if not key in translations[current_language]:
		print("警告: 翻译键不存在: ", key)
		return key
	
	var text = translations[current_language][key]
	
	# 处理格式化参数
	if args.size() > 0:
		# 检查是否是数组
		if args is Array:
			# 根据参数数量使用不同的格式化方法
			match args.size():
				1: text = text % args[0]
				2: text = text % [args[0], args[1]]
				3: text = text % [args[0], args[1], args[2]]
				4: text = text % [args[0], args[1], args[2], args[3]]
				_: print("警告: 格式化参数过多")
		else:
			# 单个参数直接格式化
			text = text % args
	
	return text

# 应用语言设置到游戏
func _apply_language():
	TranslationServer.set_locale(current_language)
	print("语言已设置为: ", current_language) 